package com.hhuc.huaweiyuntest;


import android.app.Application;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;

import com.hhuc.huaweiyuntest.constant.ConfigConstant;
import com.hhuc.huaweiyuntest.utils.DemoUtil;
import com.hhuc.huaweiyuntest.utils.PreferencesUtil;
import com.huawei.cloudlink.db.DBConfig;
import com.huawei.cloudlink.openapi.HWMSdk;
import com.huawei.cloudlink.openapi.OpenSDKConfig;
import com.huawei.cloudlink.openapi.api.param.JoinConfParam;
import com.huawei.cloudlink.openapi.api.param.LoginParam;
import com.huawei.cloudlink.openapi.api.param.PasswordJoinConfParam;
import com.huawei.cloudlink.openapi.api.param.RandomJoinConfParam;
import com.huawei.hwmbiz.eventbus.LoginResult;
import com.huawei.hwmconf.presentation.error.ErrorMessageFactory;
import com.huawei.hwmconf.sdk.util.Utils;
import com.huawei.hwmfoundation.HwmContext;
import com.huawei.hwmfoundation.callback.HwmCallback;
import com.huawei.hwmfoundation.callback.HwmCancelableCallBack;

import static com.huawei.hwmcommonui.ui.popup.loading.GlobalLoadingController.dismissLoading;

public class TestActivity extends AppCompatActivity {
    TextView accountView;
    TextView passwordView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);




        Button joinBtn = findViewById(R.id.join_btn);
        joinBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
                joinConf();
            }
        });
    }

    private void login() {
        accountView = findViewById(R.id.account);
        passwordView = findViewById(R.id.password);
        String account = accountView.getText().toString();
        String passwords = passwordView.getText().toString();
        if (TextUtils.isEmpty(account) || TextUtils.isEmpty(passwords)) {
            DemoUtil.showToast("账号和密码不能为空");
            return;
        }
        PreferencesUtil.setStringPreferences(this.getApplicationContext(),
                ConfigConstant.KEY_LOGIN_NAME, account);
        PreferencesUtil.setStringPreferences(this.getApplicationContext().getApplicationContext(),
                ConfigConstant.KEY_LOGIN_PWD, passwords);
        //   showLoading();
        LoginParam loginParam = new LoginParam()
                .setAccount(account)
                .setPassword(passwords);

        HWMSdk.getOpenApi((Application) this.getApplicationContext()).login(loginParam, new HwmCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                dismissLoading();
                //   dismiss();
                if (loginResult != null) {
                    DemoUtil.showToast("用户密码验证成功" + loginResult.getUserUuid());
                } else {
                    DemoUtil.showToast("准备进入会议");
                }
            }

            @Override
            public void onFailed(int retCode, String desc) {
                HwmContext.getInstance().runOnMainThread(() -> {
                    dismissLoading();
                    //   DemoUtil.showToast("登录错误：" + DemoUtil.getErrMsg(context, retCode));
                    //   dismiss();
                });
            }
        });
    }

        private void joinConf() {

            //   showLoading();


            TextView idView = findViewById(R.id.meeting_id);
            TextView name = findViewById(R.id.meeting_nickname);
            TextView passwordView = findViewById(R.id.meeting_pass);
            EditText randomView = findViewById(R.id.meeting_random);

            String mId = idView.getText().toString();
            String nickName = name.getText().toString();
            String random = randomView.getText().toString();
            String password = passwordView.getText().toString();
            if (TextUtils.isEmpty(mId)) {
                DemoUtil.showToast("会议ID不能为空");
                return;
            }

            Switch enableMicSwitch = findViewById(R.id.switch_enable_mic);
            boolean enableMic = enableMicSwitch.isChecked();
            Switch enableCameraSwitch =findViewById(R.id.switch_enable_camera);
            boolean enableCamera = enableCameraSwitch.isChecked();
         //   showLoading();
//加入会议
            JoinConfParam joinConfParam;
            if (!TextUtils.isEmpty(random)) {
                joinConfParam = new RandomJoinConfParam()
                        .setRandom(random)
                        .setConfId(mId)
                        .setNickname(nickName)
                        .setCameraOn(enableCamera)
                        .setMicOn(enableMic);
            } else {
                joinConfParam = new PasswordJoinConfParam()
                        .setPassword(password)
                        .setConfId(mId)
                        .setNickname(nickName)
                        .setCameraOn(enableCamera)
                        .setMicOn(enableMic);
                HWMSdk.getOpenApi(getApplication()).joinConf(joinConfParam, new HwmCancelableCallBack<Void>() {
                    @Override
                    public void onSuccess(Void ret) {
                        dismissLoading();
//                    dismiss();
                    }

                    @Override
                    public void onFailed(int retCode, String desc) {
                        dismissLoading();
//                    dismiss();
                        String err = ErrorMessageFactory.create(Utils.getApp(), retCode);
                        if (TextUtils.isEmpty(err)) {
                            err = Utils.getResContext().getString(R.string.hwmconf_join_fail_tip);
                        }
                        DemoUtil.showToast("加入会议失败: " + err);
                    }

                    @Override
                    public void onCancel() {
                        dismissLoading();
//                    dismiss();
                    }
                });
            }

        }
}